package com.ruoyi.system.controller;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.ExamClass;
import com.ruoyi.system.service.IExamClassService;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ExamStudent;
import com.ruoyi.system.service.IExamStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生Controller
 * 
 * @author ruoyi
 * @date 2023-07-30
 */
@Controller
@RequestMapping("/system/student")
public class ExamStudentController extends BaseController
{
    private String prefix = "system/student";

    @Autowired
    private IExamStudentService examStudentService;

    @Autowired
    private IExamClassService examClassService;

    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学生列表
     */
    @RequiresPermissions("system:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamStudent examStudent)
    {
        startPage();
        List<ExamStudent> list = examStudentService.selectExamStudentList(examStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @RequiresPermissions("system:student:export")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamStudent examStudent)
    {
        List<ExamStudent> list = examStudentService.selectExamStudentList(examStudent);
        ExcelUtil<ExamStudent> util = new ExcelUtil<ExamStudent>(ExamStudent.class);
        return util.exportExcel(list, "学生数据");
    }

    /**
     * 新增学生
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        //查询班级信息并且带回数据
        List<ExamClass> classes = examClassService.selectExamClassList(null);
        model.addAttribute("classes",classes);
        return prefix + "/add";
    }

    /**
     * 新增保存学生
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamStudent examStudent)
    {
        //如果没有密码就截取身份证后6
        if(examStudent.getPwd()==null||"".equals(examStudent.getPwd())) {
            examStudent.setPwd(examStudent.getIdcard().substring(12,18));
        }
        //加密MD5
        String salt = UUID.randomUUID().toString();
        String pwd = Md5Crypt.apr1Crypt(examStudent.getPwd(),salt);
        examStudent.setPwd(pwd);
        examStudent.setSalt(salt);
        // pwd+salt==>pwd === pwd \

        return toAjax(examStudentService.insertExamStudent(examStudent));
    }

    /**
     * 修改学生
     */
    @RequiresPermissions("system:student:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamStudent examStudent = examStudentService.selectExamStudentById(id);
        mmap.put("examStudent", examStudent);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamStudent examStudent)
    {
        return toAjax(examStudentService.updateExamStudent(examStudent));
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examStudentService.deleteExamStudentByIds(ids));
    }
}
